﻿using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Media;
using Launch.Views;
using MahApps.Metro.Controls;
using MahApps.Metro.IconPacks;
using ZCommon;
using ZCommon.UiExtension;

namespace Launch;

/// <summary>
///     Interaction logic for MainWindow.xaml
/// </summary>
public partial class MainWindow : MetroWindow
{
    // private readonly MainWindowVm _vm;
    // 日志中心
    private RichTextBox _textLogCenter;

    public MainWindow()
    {
        InitializeComponent();

        // _vm = DataContext as MainWindowVm;

        // 连接并注册日志中心
        ZLogSink.ZLogEvent += (level, message) =>
        {
            Application.Current.Dispatcher.Invoke(() =>
            {
                // 根据日志级别设置不同的颜色和样式
                var color = level switch
                {
                    "Debug" => Brushes.Gray,
                    "Information" => Brushes.Black,
                    "Warning" => Brushes.Orange,
                    "Error" => Brushes.Red,
                    "Fatal" => Brushes.Red,
                    _ => Brushes.Black
                };
                _textLogCenter.AppendTextWithAutoScroll($"[{level}] {message}", color);
                // _textLogCenter.ScrollToEnd();

                // using (MemoryStream stream = new MemoryStream(Encoding.UTF8.GetBytes(message)))
                // {
                //     TextRange range = new TextRange(_textLogCenter.Document.ContentStart,
                //         _textLogCenter.Document.ContentEnd);
                //     range.ApplyPropertyValue(TextElement.ForegroundProperty, color);
                //     range.Load(stream, DataFormats.Rtf);
                // }
            });
        };
    }

    private void BtnLeft_OnClick(object sender, RoutedEventArgs e)
    {
        Console.WriteLine("Write By Zr");

        // var d = new TextRange(_textLogCenter.Document.ContentStart, _textLogCenter.Document.ContentEnd).Text;
        // Console.WriteLine(d.Trim());

        // 添加一个新的菜单项,名为Test0
        var items = (HamburgerMenuControl.ItemsSource as HamburgerMenuItemCollection)!;
        var item = new HamburgerMenuIconItem
        {
            Label = "Test0",
            Icon = new PackIconMaterial {Kind = PackIconMaterialKind.Home},
            Tag = new Page1(),
        };
        items.Add(item);
        
        
    }

    private void BtnRight_OnClick(object sender, RoutedEventArgs e)
    {
        Console.WriteLine("Good Luck for You");

        // 处理名为Test0的项
        var items = (HamburgerMenuControl.ItemsSource as HamburgerMenuItemCollection)!;
        for (var i = items.Count - 1; i >= 0; --i)
        {
            Console.WriteLine($"{i}");
            if (items[i] is HamburgerMenuIconItem iconItem && iconItem.Label == "Test0")
            {
                // 改变图标
                iconItem.Icon= new PackIconMaterial {Kind = PackIconMaterialKind.Account};
                
                // 移除该项
                // items.RemoveAt(i);
                // Console.WriteLine($"removed {i},cursize {items.Count}");
            }
        }
    }

    /// <summary>
    ///     点击左侧的侧边栏项,切换右侧的页面
    /// </summary>
    private void OnItemInvoked(object sender, HamburgerMenuItemInvokedEventArgs args)
    {
        HamburgerMenuControl.Content = args.InvokedItem;
    }

    /// <summary>
    /// 当日志中心加载完成
    /// </summary>
    private void TextLogCenter_Loaded(object sender, RoutedEventArgs e)
    {
        // 记录日志中心,因为使用的是 mah:HamburgerMenu,无法正常用名字获取,所以需要在加载完成后获取
        _textLogCenter = sender as RichTextBox;
        _textLogCenter?.SetupAutoScrollBehavior();
    }
}